﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Drawing;

namespace SlimCode.SlimPasswords
{
  internal class Settings : INotifyPropertyChanged
  {
    public Settings()
    {
      m_lastStore = System.IO.Path.Combine(
        Environment.GetFolderPath( Environment.SpecialFolder.Personal ),
        "Passwords.skps" );
    }

    #region AutoMinimize PROPERTY

    private const string AutoMinimizeProperty = "AutoMinimize";

    private bool m_autoMinimize;

    public bool AutoMinimize
    {
      get { return m_autoMinimize; }
      set
      {
        if( value != m_autoMinimize )
        {
          m_autoMinimize = value;
          this.OnAutoMinimizeChanged();
        }
      }
    }

    protected virtual void OnAutoMinimizeChanged()
    {
    }

    #endregion

    #region AutoUpdateCharCount PROPERTY

    private const string AutoUpdateCharCountProperty = "AutoUpdateCharCount";

    private int m_autoUpdateCharCount = 3;

    public int AutoUpdateCharCount
    {
      get { return m_autoUpdateCharCount; }
      set
      {
        if( value < 0 )
          value = 0;
        else if( value > 20 )
          value = 20;

        if( value != m_autoUpdateCharCount )
        {
          m_autoUpdateCharCount = value;
          this.OnAutoUpdateCharCountChanged();
        }
      }
    }

    protected virtual void OnAutoUpdateCharCountChanged()
    {
    }

    #endregion

    #region LastStore PROPERTY

    private const string LastStoreProperty = "LastStore";

    private string m_lastStore = string.Empty;

    public string LastStore
    {
      get { return m_lastStore; }
      set
      {
        if( value == null )
          value = string.Empty;

        if( value != m_lastStore )
        {
          m_lastStore = value;
          this.OnLastStoreChanged();
        }
      }
    }

    protected virtual void OnLastStoreChanged()
    {
      this.OnPropertyChanged( LastStoreProperty );
    }

    #endregion

    #region PasswordScheme PROPERTY

    private const string PasswordSchemeProperty = "PasswordScheme";

    private PasswordScheme m_scheme = new PasswordScheme( true );

    public PasswordScheme PasswordScheme
    {
      get { return m_scheme; }
    }

    #endregion

    #region Version PROPERTY

    public string Version
    {
      get { return VersionInfo.StaticVersion; }
    }

    #endregion

    public bool Load( string filename )
    {
      try
      {
        XmlDocument document = new XmlDocument();

        document.Load( filename );

        XmlNode rootNode = document.SelectSingleNode( "slimPASSWORDS/Settings" );

        if( rootNode != null )
        {
          XmlNode valueNode = rootNode.SelectSingleNode( LastStoreProperty );

          if( valueNode != null )
          {
            this.LastStore = valueNode.InnerText;
          }

          valueNode = rootNode.SelectSingleNode( AutoUpdateCharCountProperty );

          if( valueNode != null )
          {
            this.AutoUpdateCharCount = XmlConvert.ToInt32( valueNode.InnerText );
          }

          valueNode = rootNode.SelectSingleNode( AutoMinimizeProperty );

          if( valueNode != null )
          {
            this.AutoMinimize = XmlConvert.ToBoolean( valueNode.InnerText );
          }

          valueNode = rootNode.SelectSingleNode( PasswordSchemeProperty );

          if( valueNode != null )
          {
            m_scheme.Load( valueNode );
          }
        }

        return true;
      }
      catch { }

      return false;
    }

    public void Save( string filename )
    {
      try
      {
        string folder = System.IO.Path.GetDirectoryName( filename );

        if( !System.IO.Directory.Exists( folder ) )
        {
          System.IO.Directory.CreateDirectory( folder );
        }

        XmlWriterSettings settings = new XmlWriterSettings();

        settings.Indent = true;
        settings.IndentChars = "  ";

        using( XmlWriter writer = XmlWriter.Create( filename, settings ) )
        {
          writer.WriteStartElement( "slimPASSWORDS" );
          {
            writer.WriteStartElement( "Settings" );
            {
              writer.WriteElementString( "Version", this.Version );
              writer.WriteElementString( LastStoreProperty, this.LastStore );
              writer.WriteElementString( AutoUpdateCharCountProperty, XmlConvert.ToString( this.AutoUpdateCharCount ) );
              writer.WriteElementString( AutoMinimizeProperty, XmlConvert.ToString( this.AutoMinimize ) );

              writer.WriteStartElement( PasswordSchemeProperty );
              {
                m_scheme.Save( writer );
              }
              writer.WriteEndElement();
            }
            writer.WriteEndElement();
          }
          writer.WriteEndElement();
        }
      }
      catch { }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged( string propertyName )
    {
      PropertyChangedEventHandler handlers = this.PropertyChanged;

      if( handlers != null )
      {
        handlers( this, new PropertyChangedEventArgs( propertyName ) );
      }
    }

    #endregion
  }
}
